package com.lam.mall.service.impl;

import java.text.MessageFormat;
import java.util.List;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lam.common.base.page.TableHelper;
import com.lam.common.components.easyexcel.imp.model.AbstractImportBody;
import com.lam.common.components.easyexcel.imp.model.ImportEntry;
import com.lam.common.components.easyexcel.imp.model.ImportWrapper;
import com.lam.common.components.easyexcel.imp.service.AbstractImportService;
import com.lam.common.constant.CommonConstant;
import com.lam.common.context.TokenUtils;
import com.lam.common.utils.StringUtils;
import com.lam.common.validate.ValidateUtil;
import com.lam.common.validate.model.UniqueDesc;
import com.lam.common.validate.model.ValidOperType;
import com.lam.mall.app.vo.HotSearchVo;
import com.lam.mall.common.constant.MallConstant;
import com.lam.mall.entity.HotSearch;
import com.lam.mall.entity.imp.HotSearchImp;
import com.lam.mall.mapStruct.HotSearchConvert;
import com.lam.mall.mapper.HotSearchMapper;
import com.lam.mall.param.HotSearchParam;
import com.lam.mall.service.HotSearchService;

/**
 * 热搜管理Service业务层处理
 * 
 * @author admin
 * @date 2023-04-20
 */
@Service
public class HotSearchServiceImpl extends AbstractImportService<HotSearchImp, HotSearch> implements HotSearchService {

    @Autowired
    private HotSearchMapper hotSearchMapper;
    @Autowired
    private HotSearchService hotSearchService;

    @Override
    @Cacheable(cacheNames = "HotSearchVo", key = "#shopId")
    public List<HotSearchVo> getCacheByShopId(String shopId) {
    	
    	QueryWrapper<HotSearch> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("shop_id", shopId);
        queryWrapper.eq("status", CommonConstant.STATUS_NORMAL_1);
        TableHelper.orderByColumn("sort_no");
        List<HotSearch> list = hotSearchMapper.selectList(queryWrapper);
        return HotSearchConvert.instance.toVo(list);
    }

    @Override
    @CacheEvict(cacheNames = "HotSearchVo", key = "#shopId")
    public void removeCacheByShopId(String shopId) {
    }
    
    
    /**
     * 查询热搜管理
     * 
     * @param id ID
     * @return 单个对象
     */
    @Override
    @Transactional(readOnly = true)
    public HotSearch selectById(String id) {

        return hotSearchMapper.selectById(id);
    }

    /**
     * 查询热搜管理列表
     * 
     * @param param
     * @return 
     */
    @Override
    @Transactional(readOnly = true)
    public List<HotSearch> selectList(HotSearchParam param) {
    
        QueryWrapper<HotSearch> queryWrapper = new QueryWrapper<>();

        //关键字查询
        if(StringUtils.isNotBlank(param.getKeywords())) {
            queryWrapper.and(wr->{
                wr.or().like("title", param.getKeywords());
                wr.or().like("content", param.getKeywords());
            });
        }
        
        queryWrapper.eq(Objects.nonNull(param.getShopId()), "shop_id", param.getShopId());
        queryWrapper.like(StringUtils.isNotBlank(param.getTitleKey()), "title", param.getTitleKey());
        queryWrapper.eq(StringUtils.isNotBlank(param.getTitle()), "title", param.getTitle());
        queryWrapper.like(StringUtils.isNotBlank(param.getContentKey()), "content", param.getContentKey());
        queryWrapper.eq(StringUtils.isNotBlank(param.getContent()), "content", param.getContent());
        queryWrapper.eq(Objects.nonNull(param.getStatus()), "status", param.getStatus());

        return hotSearchMapper.selectList(queryWrapper);
    }

    /**
     * 新增热搜管理
     * 
     * @param hotSearch 
     * @return 
     */
    @Override
    @Transactional
    public int insert(HotSearch hotSearch) {
    
        int n = hotSearchMapper.insert(hotSearch);
        if(n>0) {
        	hotSearchService.removeCacheByShopId(hotSearch.getShopId());
        }
        return n;
    }

    /**
     * 修改热搜管理
     * 
     * @param hotSearch 
     * @return 
     */
    @Override
    @Transactional
    public int update(HotSearch hotSearch) {
    
        int n = hotSearchMapper.updateById(hotSearch);
        if(n>0) {
        	hotSearchService.removeCacheByShopId(hotSearch.getShopId());
        }
        return n;
    }

    /**
     * 删除热搜管理信息
     * 
     * @param idStr ID，多个用逗号分隔
     * @return 
     */
    @Override
    @Transactional
    public int deleteById(String idStr) {
    
        if(StringUtils.isBlank(idStr)) {
            return 0;
        }
        
        int n = hotSearchMapper.deleteBatchIds(StringUtils.str2List(idStr));
        if(n>0) {
        	hotSearchService.removeCacheByShopId(TokenUtils.getLoginUserShopId());
        }
        return n;
    }
    
    /**
     * 导入热搜管理数据
     * @param wrapper 热搜管理数据包装对象
     */
    @Override
    @Transactional
    public void importData(ImportWrapper<HotSearchImp, HotSearch> wrapper) {
        
        UniqueDesc uniqueDesc = ValidateUtil.selectUniqueDesc(MallConstant.UNIQUE_HOT_SEARCH_TITLE);
        importData(wrapper, new AbstractImportBody<HotSearchImp, HotSearch>() {
            @Override
            public HotSearch selectRecord(ImportEntry<HotSearchImp, HotSearch> entry) {
                return ValidateUtil.selectUniqueData(entry.getData(), ValidOperType.ADD, uniqueDesc, HotSearch.class);
            }
            @Override
			public int insertData(HotSearch data) {
				return insert(data);
			}
			@Override
			public int updateData(HotSearch data) {
				return update(data);
			}
            @Override
            public String notUniqueMsg(ImportEntry<HotSearchImp, HotSearch> entry) {
                return MessageFormat.format("热搜标题【{0}】已存在", entry.getData().getTitle());
            }
        });
    }
    
}
